package com.example.cryptodemo;

import com.example.cryptodemo.aes.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class Program {
    @Test
    public void test2() throws Exception {
        /*
         第三方回复公众平台

         需要填写一些信息

         包括url（接收信息的地址，带http或https）
         token
         EncodingAESKey(必须43位)
        */
        String appId = "wxbfsfgg11529c136998cb6";
        String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
        String token = "pamtest";
        String timestamp = "1409304348";
        String nonce = "xxxxxx";
        // 需要加密的明文
        String replyMsg = " 中文<xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";

        // 开发者信息

        //消息加密配置
        BizMsgCrypt pc = BizMsgCryptUtil.getBizMsgCrypt(token, encodingAesKey);
        Message mingwenMessage = BizMsgCryptUtil.getEncryptMessage(pc, replyMsg);
        System.out.println("加密后: " + ParserFactory.getMesseageStr(mingwenMessage));
        //
        // 公众平台发送消息给第三方，第三方处理
        //

        // 第三方收到公众号平台发送的消息
        String result2 = pc.decryptMsg(mingwenMessage);
        System.out.println("解密后明文: " + result2);
        Assertions.assertEquals(replyMsg, result2);
    }

    @Test
    public void generateEncodingAesKey() {
        String str = BizMsgCryptUtil.generateEncodingAesKey();
        System.out.println(str);
        Assertions.assertEquals(43, str.length());
    }

    @Test
    void getTheEncryptionKey() throws AesException {
        String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
        String token = "OFQoGBtW3X";
        BizMsgCrypt pc = BizMsgCryptUtil.getBizMsgCrypt(token, encodingAesKey);
        String str1 = BizMsgCryptUtil.getTheEncryptionKey(pc);
        System.out.println(str1);

        BizMsgCrypt pc2 = BizMsgCryptUtil.getBizMsgCrypt("OFQoGBtW3X:abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG");
        String str2 = BizMsgCryptUtil.getTheEncryptionKey(pc2);
        System.out.println(str2);

        Assertions.assertEquals(str1, str2);
    }

    @Test
    public void encodeTest() throws UnsupportedEncodingException {
        String uri="http://localhost:8080/crypto?signature=557d331b4f7eb10b0ab4b175738bc91b6b2ae04b&timeStamp=1636607552786&nonce=j315rD&echoStr=ekyskcAnzIUaIJxo6YQxl+O02D5Wwoi/gWwBYW5dk3o%3D";
        String a = URLEncoder.encode("+", "utf-8");
        System.out.println(a);

    }
}
